//
//  DYResultPickerView.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/19.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYResultPickerView.h"


@interface DYResultPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,strong)UIToolbar *toolBar;
@property (nonatomic,copy)NSString *seletedStr;

@end

@implementation DYResultPickerView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    if (self) {
        [self initView];
    }
    return self;
}
-(void)initView
{
    
    self.backgroundColor=[UIColor colorWithWhite:0 alpha:0.5];
    self.toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,kScreenHeight,kScreenWidth,45)];
    self.toolBar.barStyle=UIBarStyleDefault;
    self.toolBar.barTintColor=[UIColor xhq_base];
    [self addSubview:self.toolBar];
    
    self.titleButton = [[UIBarButtonItem alloc] initWithTitle:@"统计分类" style: UIBarButtonItemStylePlain target: nil action: nil];
    self.titleButton.tintColor=WhiteColor;
    [self.titleButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:18],NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"确认  " style: UIBarButtonItemStyleDone target: self action: @selector(done)];
    rightButton.tintColor=WhiteColor;
    
    UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:@selector(cancel)];
    UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
    NSArray *array = [[NSArray alloc] initWithObjects: leftButton,fixedButton, self.titleButton,fixedButton, rightButton, nil];
    
    
    [self.toolBar setItems:array];
    [self addSubview:self.pickerView];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    tap.delegate=self;
    [self addGestureRecognizer:tap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.toolBar]||[touch.view isDescendantOfView:self.pickerView]) {
        return NO;
    }
    return  YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataArray.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.dataArray[row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.seletedStr=self.dataArray[row];
    
}

-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray=dataArray;
    
    [self.pickerView reloadAllComponents];
    self.seletedStr=[dataArray firstObject];
}

-(void)tap
{
    [UIView animateWithDuration:0.5f animations:^{
        self.pickerView.frame=CGRectMake(0,self.xhq_height, kScreenWidth,self.pickerView.xhq_height);
        self.toolBar.frame=CGRectMake(0, self.xhq_height, kScreenWidth, self.toolBar.xhq_height);
        
    } completion:^(BOOL finished) {
        self.hidden=YES;
        [self removeFromSuperview];
    }];
}
-(void)show
{
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    [UIView animateWithDuration:0.5f animations:^{
        self.toolBar.frame=CGRectMake(0,self.xhq_height-(216+45),kScreenWidth,45);
        self.pickerView.frame=CGRectMake(0,self.xhq_height-216, kScreenWidth,216);
        self.hidden=NO;
    }];
}
-(void)done
{
    if (self.pickerViewBlock) {
        self.pickerViewBlock(self.seletedStr);
    }
    
    [UIView animateWithDuration:0.5f animations:^{
        self.pickerView.frame=CGRectMake(0,self.xhq_height, kScreenWidth,self.pickerView.xhq_height);
        self.toolBar.frame=CGRectMake(0, self.xhq_height, kScreenWidth, self.toolBar.xhq_height);
        
    } completion:^(BOOL finished) {
        self.hidden=YES;
        [self removeFromSuperview];
    }];
}

-(void)setToolbarTitle:(NSString *)toolbarTitle
{
    _toolbarTitle=toolbarTitle;
    
    [self.titleButton  setTitle:toolbarTitle];
}

-(UIPickerView *)pickerView
{
    if (!_pickerView) {
        _pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0,kScreenHeight,kScreenWidth,BILIHEIGHT(200))];
        _pickerView.delegate=self;
        _pickerView.dataSource=self;
        _pickerView.backgroundColor=[UIColor whiteColor];
    }
    return _pickerView;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
